package com.heima.article.feign;

import com.heima.apis.article.IArticleClient;
import com.heima.article.service.ApArticleLikesService;
import com.heima.article.service.ApArticleService;
import com.heima.article.service.ApCollectionService;
import com.heima.model.article.dtos.ArticleDto;
import com.heima.model.common.dtos.ResponseResult;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.lang.reflect.InvocationTargetException;

@RestController
public class ArticleClient implements IArticleClient {
    @Resource
    private ApArticleService apArticleService;
    @Resource
    private ApCollectionService apCollectionService;

    @Resource
    private ApArticleLikesService apArticleLikesService;

    @PostMapping("/api/v1/article/save")
    @Override
    public ResponseResult saveArticle(@RequestBody ArticleDto dto) throws InvocationTargetException, IllegalAccessException {

        return apArticleService.saveArticle(dto);
    }

    @Override
    @PostMapping("/api/v1/collection/collect")
    public ResponseResult collectArticle(Integer userId, Long articleId, Boolean isCollect) {
        return apCollectionService.collectArticle(userId,articleId,isCollect);
    }

    @Override
    @GetMapping("/api/v1/collection/isCollected")
    public ResponseResult isArticleCollected(Integer userId, Long articleId) {
        return apCollectionService.isArticleCollected(userId,articleId);
    }
    @Override
    @GetMapping("/api/v1/collection/userCollections")
    public ResponseResult getUserCollections(Integer userId, Integer page, Integer size) {
        return apCollectionService.getUserCollections(userId, page, size);
    }


    /**
     * 点赞或取消点赞文章
     * @param userId 用户ID
     * @param articleId 文章ID
     * @param isLike 是否点赞 true-点赞 false-取消点赞
     * @return
     */
    @PostMapping("/api/v1/article/likes/like")
    public ResponseResult likeArticle(@RequestParam Integer userId,
                                      @RequestParam Long articleId,
                                      @RequestParam Boolean isLike) {
        return apArticleLikesService.likeArticle(userId, articleId, isLike);
    }

    /**
     * 判断文章是否被点赞
     * @param userId 用户ID
     * @param articleId 文章ID
     * @return
     */
    @GetMapping("/api/v1/article/likes/isLiked")
    public ResponseResult isArticleLiked(@RequestParam Integer userId,
                                         @RequestParam Long articleId) {
        return apArticleLikesService.isArticleLiked(userId, articleId);
    }

    /**
     * 获取用户点赞文章列表
     * @param userId 用户ID
     * @param page 页码
     * @param size 每页大小
     * @return
     */
    @GetMapping("/api/v1/article/likes/user/{userId}")
    public ResponseResult getUserLikedArticles(@PathVariable Integer userId,
                                               @RequestParam(defaultValue = "1") Integer page,
                                               @RequestParam(defaultValue = "10") Integer size) {
        return apArticleLikesService.getUserLikedArticles(userId, page, size);
    }
}
